package com.ccc.dingtalk.service.dingtalk.impl;

import com.ccc.dingtalk.config.CompanyProperties;
import com.ccc.dingtalk.pojo.vo.ClockTimeVO;
import com.ccc.dingtalk.service.dingtalk.ClockService;
import com.ccc.dingtalk.service.dingtalk.TokenService;
import com.ccc.dingtalk.util.DingtalkUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @author superC
 * @version 1.0.0
 * @date 2023-08-08
 */

@Service
public class ClockServiceImpl implements ClockService {
    protected static final Logger logger = LoggerFactory.getLogger(ClockServiceImpl.class);

    @Autowired
    private CompanyProperties companyProperties;

    @Autowired
    private DingtalkUtil dingtalkUtil;

    @Autowired
    private TokenService tokenService;

    @Override
    @Cacheable(value = "clockTime", unless = "#result == null")
    public ClockTimeVO getClockTime() {
        ClockTimeVO vo = new ClockTimeVO();
        try {
            vo = dingtalkUtil.getDefalutShiftClockTime(tokenService.getAccessToken());
        } catch (Exception e) {
            logger.error("【调用钉钉接口】-> 【捕获异常-获取打卡时间】\r\n异常记录：", e);
            vo.setOnTime(companyProperties.getOnTime().trim());
            vo.setOffTime(companyProperties.getOffTime().trim());
        }
        logger.info("【调用接口】-> 获取钉钉的考勤打卡时间：{} ~ {}", vo.getOnTime(), vo.getOffTime());
        return vo;
    }

}
